﻿using BW.WMS.Application.Features.Location.Dtos;
using BW.WMS.Application.Features.Warehouse.Dtos;
using BW.WMS.Application.Features.Warehouse.Interfaces;
using BW.WMS.Core.Enums;
using BW.WMS.Core.Result;
using BW.WMS.Infrastructure.Data.Contexts;
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.ML;
using System.Text;

namespace BW.WMS.API.Controllers
{
    /// <summary>
    /// WMS仓库管理—仓库控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMS_WarehouseController : ControllerBase
    {
        private readonly IWMS_WarehouseService warehouseService;
        /// <summary>
        /// 构造函数，注入依赖服务
        /// </summary>
        /// <param name="warehouseService">仓库服务接口</param>
        public WMS_WarehouseController(IWMS_WarehouseService warehouseService)
        {
            this.warehouseService = warehouseService;
        }
        /// <summary>
        /// 获取仓库分页列表，支持多条件查询
        /// </summary>
        /// <param name="queryDto">查询条件DTO，包含关键字、状态、部门ID等筛选条件</param>
        /// <returns>仓库分页数据，包含总条数和当前页数据</returns>
        [HttpGet]
        public async Task<ApiPage<WMS_WarehouseDto>> GetWarehousePageListAsync([FromQuery]WMS_WarehouseQueryDto queryDto)
        {
            return await warehouseService.GetWarehousePageListAsync(queryDto);
        }
        /// <summary>
        /// 根据ID获取仓库详情，包括仓库基本信息和下属库位列表
        /// </summary>
        /// <param name="id">仓库ID</param>
        /// <returns>仓库详情DTO，包含基本信息和库位集合</returns>
        [HttpGet]
        public async Task<ApiResult<WMS_WarehouseDto>> GetWarehouseByIdAsync([FromQuery]long id)
        {
            return await warehouseService.GetWarehouseByIdAsync(id);
        }
        /// <summary>
        /// 创建新仓库
        /// </summary>
        /// <param name="createDto">仓库创建DTO，包含仓库的基本信息</param>
        /// <returns>操作结果，成功时返回新创建的仓库ID</returns>
        [HttpPost]
        public async Task<ApiResult<long>> CreateWarehouseAsync(WMS_WarehouseCreateDto createDto)
        {
            return await warehouseService.CreateWarehouseAsync(createDto);
        }
        /// <summary>
        /// 更新仓库信息
        /// </summary>
        /// <param name="updateDto">仓库更新DTO，包含需要更新的字段</param>
        /// <returns>操作结果，表示更新是否成功</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateWarehouseAsync(WMS_WarehouseUpdateDto updateDto)
        {
            return await warehouseService.UpdateWarehouseAsync(updateDto);
        }
        /// <summary>
        /// 删除仓库（逻辑删除）
        /// </summary>
        /// <param name="id">仓库ID</param>
        /// <returns>操作结果，表示删除是否成功</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> DeleteWarehouseAsync(long id)
        {
            return await warehouseService.DeleteWarehouseAsync(id);
        }

        /// <summary>
        /// 批量删除仓库（逻辑删除）
        /// </summary>
        /// <param name="ids">仓库ID列表</param>
        /// <returns>操作结果，表示删除是否成功</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> BatchDeleteWarehousesAsync(List<long> ids)
        {
            return await warehouseService.BatchDeleteWarehousesAsync(ids);
        }
        /// <summary>
        /// 更新仓库状态
        /// </summary>
        /// <param name="id">仓库ID</param>
        /// <param name="status">新状态值</param>
        /// <param name="updatedBy">操作人</param>
        /// <returns>操作结果，表示状态更新是否成功</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateWarehouseStatusAsync(long id, int status, string updatedBy)
        {
            return await warehouseService.UpdateWarehouseStatusAsync(id, status, updatedBy);
        }
        /// <summary>
        /// 获取所有部门列表（树形结构）
        /// </summary>
        /// <param name="parentId">父级部门ID</param>
        /// <returns>部门树形结构</returns>
        [HttpGet]
        public async Task<ApiResult<List<DepartmentTreeDto>>> GetDepartmentsTree([FromQuery] long parentId = 0)
        {
            return await warehouseService.GetAllDepartmentsTreeAsync(parentId);
        }

        /// <summary>
        /// 获取所有仓库主管列表
        /// </summary>
        /// <param name="departmentId">部门ID，可选参数</param>
        /// <returns>主管列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<KeyValuePair<long, string>>>> GetManagers([FromQuery] long? departmentId = null)
        {
            return await warehouseService.GetAllManagersAsync(departmentId);
        }
    }
}
